package net.f3322.lingwl13.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.f3322.lingwl13.domain.Book;
import entity.PageResult;
import net.f3322.lingwl13.domain.Record;
import net.f3322.lingwl13.domain.User;
import net.f3322.lingwl13.mapper.BookMapper;
import net.f3322.lingwl13.service.BookService;
import net.f3322.lingwl13.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private RecordService recordService;
    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page = bookMapper.selectNewBooks();
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Book findById(String id) {
        Book book = bookMapper.findById(id);
        return book;
    }

    @Override
    public Integer borrowBook(Book book) {
        Book b = this.findById(book.getId()+"");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        book.setBorrowTime(dateFormat.format(new Date()));
        book.setStatus("1");
        book.setPrice(b.getPrice());
        book.setUploadTime(b.getUploadTime());
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page = bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer addBook(Book book) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        book.setUploadTime(dateFormat.format(new Date()));
        return bookMapper.addBook(book);
    }

    @Override
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page;
        book.setBorrower(user.getName());
        if("ADMIN".equals(user.getRole())||"TestADMIN".equals(user.getRole())){
            page = bookMapper.selectBorrowed(book);
        }else{
            page = bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public boolean returnBook(String id, User user) {
        Book book = this.findById(id);
        boolean rb = book.getBorrower().equals(user.getName());
        if (rb) {
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return rb;
    }

    @Override
    public Integer returenConfirm(String id) {
        Book book = this.findById(id);
        Record record = this.setRecord(book);
        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        Integer count = bookMapper.editBook(book);
        if(count==1){
            return recordService.addRecord(record);
        }
        return 0;
    }

    @Override
    public Record setRecord(Book book) {
        Record record=new Record();
        record.setBookname(book.getName());
        record.setBookisbn(book.getIsbn());
        record.setBorrower(book.getBorrower());
        record.setBorrowTime(book.getBorrowTime());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        record.setRemandTime(dateFormat.format(new Date()));
        return record;
    }

}
